package tuiLayer;

import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;

public class PersonUI {
   private PersonCtrl persCtrl;
    
   public PersonUI() {
       persCtrl = new PersonCtrl();
    }
    
   /**
     * Friends menu methods
     */
    public void friendsMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeFriendsMenu();
            
            switch(choice) {
            case 1 : createPerson();
                    stop = true;
                break;
            case 2 : findPerson();
                break;
            case 3 : updatePerson();
                break;
            case 4 : removePerson();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    } 
    
    private int writeFriendsMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Friends Menu ***");
        System.out.println("1. Add person ");
        System.out.println("2. Find person ");
        System.out.println("3. Update person ");
        System.out.println("4. Remove person ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    /**
     * Friends information input
     */
    private String inputName() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the name : ");
        String name = keyboard.nextLine();
        return name;
    }
    
    private String inputAddress() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the address : ");
        String address = keyboard.nextLine();
        return address;
    }
    
    private int inputPostalCode() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the postal code (numeric value) : ");
        int postalCode = keyboard.nextInt();
        return postalCode;
    }
    
    private String inputCity() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the city : ");
        String city = keyboard.nextLine();
        return city;
    }
    
    public String inputPhone() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the phone number of the person : ");
        String phone = keyboard.nextLine();
        return phone;
    }
    
    /**
     * Friends information show
     */
    private void showName(String name)
    {
        System.out.println("Name : " + name);
    }
    
    private void showAddress(String address)
    { 
        System.out.println("Address : " + address);
    }
    
    private void showPostalCode(int postalCode)
    { 
        System.out.println("Postal Code : " + postalCode);
    }

    private void showCity(String city)
    { 
        System.out.println("City : " + city);
    }
    
    /**
     * Friends find person
     */
    private void findPerson() {
        String phone = inputPhone();
        Person p = (persCtrl.findPerson(phone));
        if(p != null) {
            System.out.println();
            showName(p.getName());
            showAddress(p.getAddress());
            showPostalCode(p.getPostalCode());
            showCity(p.getCity());
            
        }
        else { 
            System.out.println("No person was found with the specified phone number.");
        }
        System.out.println();
        MainMenuUI.pause();
        
    }
    
    /**
     * Update person
     */
    private void updatePerson() {
        Scanner keyboard = new Scanner(System.in);
        String phone = inputPhone();
        Person p = (persCtrl.findPerson(phone));           
        boolean stop = false;
        if (p != null) {
            System.out.println();
            showName(p.getName());
            showAddress(p.getAddress());
            showPostalCode(p.getPostalCode());
            showCity(p.getCity());
            System.out.println();
            System.out.println("1. Update name");
            System.out.println("2. Update address");
            System.out.println("3. Update postal code");
            System.out.println("4. Update city");
            System.out.println("5. Update phone");
            System.out.println("0. Return to previous menu");
            System.out.println();
            System.out.println("Enter your choice : ");
            
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : p.setName(inputName());
                    break;
                case 2 : p.setAddress(inputAddress());
                    break;
                case 3 : p.setPostalCode(inputPostalCode());
                    break;
                case 4 : p.setCity(inputCity());
                    break;
                case 5 : p.setPhone(inputPhone());
                    break;
                case 0 : stop= true;
                }
                System.out.println();
                System.out.println("The information was updated");
                System.out.println();
            }
            
        }
        else {
            System.out.println("No person was found with the specified phone number.");
        }
        MainMenuUI.pause();
    }
    
    /**
     * Friends remove person
     */
    private void removePerson() {
        Scanner keyboard = new Scanner(System.in);
        String phone = inputPhone();
        Person p = (persCtrl.findPerson(phone));
        boolean stop = false;
        if (p != null) {
            System.out.println();
            showName(p.getName());
            showAddress(p.getAddress());
            showPostalCode(p.getPostalCode());
            showCity(p.getCity());
            
            System.out.println();
            System.out.println("1.Remove person");
            System.out.println("0.Return to previous menu");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : persCtrl.removePerson(p);
                        System.out.println();
                        System.out.println("The person was successfully removed");
                        System.out.println();
                        MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
                }
            }
        }
        else {
            System.out.println("No person was found with the specified phone number.");
        }
    }
    
    /**
     * Friends crerate person
     */
    private void createPerson() {
        String name = inputName();
        String address = inputAddress();
        int postalCode = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();
        
        persCtrl.createPerson(name, address, postalCode, city, phone);
        System.out.println();
        System.out.println("The person was successfully created");
        System.out.println();
        MainMenuUI.pause();
    }
}
